package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.IotGateway;
import com.ruoyi.system.mapper.IotGatewayMapper;
import com.ruoyi.system.service.SendInstructsService;
import com.ruoyi.system.utils.ElectricityMeterUtil;
import com.ruoyi.system.utils.InstructConstant;
import com.ruoyi.system.utils.InstructTypeConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotDeviceMapper;
import com.ruoyi.system.domain.IotDevice;
import com.ruoyi.system.service.IIotDeviceService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 物联设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-29
 */
@Service
public class IotDeviceServiceImpl implements IIotDeviceService 
{
    @Autowired
    private IotDeviceMapper iotDeviceMapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    SendInstructsService sendInstructsService;
    @Resource
    IotGatewayMapper iotGatewayMapper;

    /**
     * 查询物联设备
     * 
     * @param id 物联设备主键
     * @return 物联设备
     */
    @Override
    public IotDevice selectIotDeviceById(Integer id)
    {
        return iotDeviceMapper.selectIotDeviceById(id);
    }

    /**
     * 查询物联设备列表
     * 
     * @param iotDevice 物联设备
     * @return 物联设备
     */
    @Override
    public List<IotDevice> selectIotDeviceList(IotDevice iotDevice)
    {
        return iotDeviceMapper.selectIotDeviceList(iotDevice);
    }

    /**
     * 新增物联设备
     * 
     * @param iotDevice 物联设备
     * @return 结果
     */
    @Override
    public int insertIotDevice(IotDevice iotDevice)
    {
        iotDevice.setCreateTime(DateUtils.getNowDate());
        return iotDeviceMapper.insertIotDevice(iotDevice);
    }

    /**
     * 修改物联设备
     * 
     * @param iotDevice 物联设备
     * @return 结果
     */
    @Override
    public int updateIotDevice(IotDevice iotDevice)
    {
        return iotDeviceMapper.updateIotDevice(iotDevice);
    }

    /**
     * 批量删除物联设备
     * 
     * @param ids 需要删除的物联设备主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceByIds(Integer[] ids)
    {
        return iotDeviceMapper.deleteIotDeviceByIds(ids);
    }

    /**
     * 删除物联设备信息
     * 
     * @param id 物联设备主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceById(Integer id)
    {
        return iotDeviceMapper.deleteIotDeviceById(id);
    }

    @Override
    public int handleGateSwitch(Map<String, String> paramMap, HttpServletRequest request) throws Exception {
        String deviceCode=paramMap.get("deviceCode");
        String type=redisCache.getCacheObject(InstructConstant.handleInstructs+deviceCode);
        if (!StringUtils.isEmpty(type)){
            throw new Exception("请稍后再试");
        }
        String electricityStatus=paramMap.get("electricityStatus");
        Long gatewayId= Long.valueOf(paramMap.get("gatewayId"));
        IotGateway iotGateway = iotGatewayMapper.selectIotGatewayById(gatewayId);
        if (!StringUtils.isEmpty(electricityStatus)){
            sendElectricityStatus(electricityStatus,deviceCode,iotGateway,request);
            return 1;
        }
        return 0;
    }
    private void sendElectricityStatus(String status, String deviceCode, IotGateway gateway,HttpServletRequest request){
        String address="";
        String instruce="";
        String newIp="";
        address= ElectricityMeterUtil.deviceAddressConvert(deviceCode);
        newIp="/"+gateway.getGatewayIp()+":"+gateway.getGatewayPort();
        if ("1".equals(status)){
            instruce= ElectricityMeterUtil.getCompleteInstruct(address,InstructConstant.ELECTRICITY_METER_ON_INSTRUCT);
            sendInstructsService.sendInstructs(instruce,newIp,request,deviceCode, InstructTypeConstant.E_MAIN_GATE_NO.getKey());
        }else{
            instruce=ElectricityMeterUtil.getCompleteInstruct(address,InstructConstant.ELECTRICITY_METER_PULL_INSTRUCT);
            sendInstructsService.sendInstructs(instruce,newIp,request,deviceCode, InstructTypeConstant.E_MAIN_GATE_OFF.getKey());
        }
    }

//    @Override
//    public Object[] sendInstructs(String context, String ip, HttpServletRequest request, String deviceCode, String operate) {
//        try {
//            Long countTimeLong=0L;
//            a:while (true){
//                Map<String, Object> cacheMap = redisCache.getCacheMap(InstructConstant.handleInstructs + deviceCode);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    @Override
//    public void callbackInstructResultMsg(String clientIp, String str) {
//
//    }
//
//    @Override
//    public void handleGateWayStatus(String clientIp, Object o, boolean b) {
//
//    }

}
